package cn.shadow.module.snake;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class FoodNode extends Actor {
    public static final int WORLD_WIDTH = 1080;
    public static final int WORLD_HEIGHT = 900;
    private float rad;
    private TextureRegion region;

    public FoodNode(float x, float y, TextureRegion region) {
        this.rad = region.getRegionWidth() / 2.0f;
        this.region = region;
        setPosition(x, y);
        setSize(this.region.getRegionWidth(), this.region.getRegionHeight());
    }

    public TextureRegion getRegion() {
        return region;
    }

    public void setRegion(TextureRegion region) {
        this.region = region;
    }

    public static FoodNode creatFood(float x, float y, TextureRegion region) {
        float randX = (int) (Math.random() * (x - region.getRegionWidth()*2.0f)) + region.getRegionWidth()/2.0f;
        float randY = (int) (Math.random() * (y - region.getRegionHeight()*2.0f)) + region.getRegionHeight()/2.0f;
        return new FoodNode(randX, randY, region);
    }


    /**
     * 是否可以吃到
     *
     * @param x
     * @param y
     * @return
     */
    public boolean canEat(float x, float y, float rad) {
        double absX = Math.abs(getX() - x);
        double absY = Math.abs(getY() - y);
        return Math.sqrt(absX * absX + absY * absY) <= this.rad + rad;
    }

    public float getRad() {
        return rad;
    }

    public void setRad(float rad) {
        this.rad = rad;
    }


    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        // 如果 region 为 null 或者 演员不可见, 则直接不绘制
        if (region == null || !isVisible()) {
            return;
        }
        batch.draw(
                region,
                getX(), getY(),
                getOriginX(), getOriginY(),
                getWidth(), getHeight(),
                getScaleX(), getScaleY(),
                getRotation()
        );
    }
}
